package com.atguigu.tingshu.search.service.impl;

import com.atguigu.tingshu.album.client.AlbumInfoFeignClient;
import com.atguigu.tingshu.album.client.CategoryFeignClient;
import com.atguigu.tingshu.common.constant.RedisConstant;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.model.album.AlbumInfo;
import com.atguigu.tingshu.model.album.BaseCategoryView;
import com.atguigu.tingshu.search.service.ItemService;
import com.atguigu.tingshu.user.client.UserInfoFeignClient;
import com.atguigu.tingshu.vo.album.AlbumStatVo;
import com.atguigu.tingshu.vo.user.UserInfoVo;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RBloomFilter;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadPoolExecutor;

@Slf4j
@Service
@SuppressWarnings({"all"})
public class ItemServiceImpl implements ItemService {

    @Autowired
    private AlbumInfoFeignClient albumInfoFeignClient;

    @Autowired
    private CategoryFeignClient categoryFeignClient;

    @Autowired
    private UserInfoFeignClient userInfoFeignClient;

    @Autowired
    private ThreadPoolExecutor threadPoolExecutor;

    @Autowired
    private RedissonClient redissonClient;
    @Override
    public Map<String, Object> getItem(Long albumId) {
        /*
        result.put("albumInfo", albumInfo);			获取专辑信息
        result.put("albumStatVo", albumStatVo);		获取专辑统计信息
        result.put("baseCategoryView", baseCategoryView);	获取分类信息
        result.put("announcer", userInfoVo);	获取主播信息

        多线程：跟专辑上架一样.
         */
        Map<String, Object> result = new HashMap<>();
        //  判断当前布隆过滤器中是否有专辑Id
//        RBloomFilter<Object> bloomFilter = this.redissonClient.getBloomFilter(RedisConstant.ALBUM_BLOOM_FILTER);
//        if (!bloomFilter.contains(albumId)){
//            //  不包含直接返回空对象
//            return result;
//        }

        //  根据专辑Id获取专辑数据.
        CompletableFuture<AlbumInfo> albumInfoCompletableFuture = CompletableFuture.supplyAsync(() -> {
            Result<AlbumInfo> infoResult = albumInfoFeignClient.getAlbumInfo(albumId);
            Assert.notNull(infoResult, "返回专辑结果集为空");
            AlbumInfo albumInfo = infoResult.getData();
            Assert.notNull(albumInfo, "返回专辑对象为空");
            result.put("albumInfo", albumInfo);
            //  返回数据
            return albumInfo;
        },threadPoolExecutor);

        //  获取统计信息。
        CompletableFuture<Void> statCompletableFuture = CompletableFuture.runAsync(() -> {
            //  远程调用
            Result<AlbumStatVo> albumStatVoResult = albumInfoFeignClient.getAlbumStatVo(albumId);
            Assert.notNull(albumStatVoResult, "返回专辑统计结果集为空");
            AlbumStatVo albumStatVo = albumStatVoResult.getData();
            Assert.notNull(albumStatVo, "返回专辑统计对象为空");
            result.put("albumStatVo", albumStatVo);
        },threadPoolExecutor);

        //  获取分类
        CompletableFuture<Void> categoryCompletableFuture = albumInfoCompletableFuture.thenAcceptAsync(albumInfo -> {
            //  远程调用
            Result<BaseCategoryView> categoryViewResult = categoryFeignClient.getCategoryView(albumInfo.getCategory3Id());
            Assert.notNull(categoryViewResult, "返回专辑分类结果集为空");
            BaseCategoryView baseCategoryView = categoryViewResult.getData();
            Assert.notNull(baseCategoryView, "返回专辑分类对象为空");
            result.put("baseCategoryView", baseCategoryView);
        },threadPoolExecutor);

        //  获取主播信息
        CompletableFuture<Void> userCompletableFuture = albumInfoCompletableFuture.thenAcceptAsync(albumInfo -> {
            //  远程调用
            Result<UserInfoVo> userInfoVoResult = userInfoFeignClient.getUserInfoVo(albumInfo.getUserId());
            Assert.notNull(userInfoVoResult, "返回主播结果集为空");
            UserInfoVo userInfoVo = userInfoVoResult.getData();
            Assert.notNull(userInfoVo, "返回主播对象为空");
            result.put("announcer", userInfoVo);
        },threadPoolExecutor);

        //  多任务组合
        CompletableFuture.allOf(
                albumInfoCompletableFuture,
                categoryCompletableFuture,
                userCompletableFuture,
                statCompletableFuture
        ).join();
        //  返回数据
        return result;
    }
}
